﻿using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Mapping.ByCode;
using Xenta.Enums;
using Xenta.Utils;

namespace Xenta.Entities
{
    /// <summary>
    /// Represents the attribute entity.
    /// </summary>
    /// <remarks><pre>
    /// The entity regarding the disclosure level.
    /// 
    /// + Entire
    ///  + Extended
    ///   + Base
    ///    | - DisplayOrder
    ///    | - Flags
    ///    | - UpdatedOn
    ///    | - CreatedOn
    ///    | - Translations(of the same level)
    ///    + Keys
    ///     + Primary
    ///      | - EntityID
    /// 
    /// Note: upper level includes lower level.
    /// </pre></remarks>
    public class LookupAttributeEntity : AuditableEntity
    {
        #region Fields

        private ICollection<TLookupAttributeEntity> _translations;

        #endregion

        #region Ctors

        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public LookupAttributeEntity()
        {
            _translations = new List<TLookupAttributeEntity>();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the display order.
        /// </summary>
        public virtual int DisplayOrder
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public virtual LookupAttributeFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Collections

        /// <summary>
        /// Gets or sets the attribute translations.
        /// </summary>
        public virtual ICollection<TLookupAttributeEntity> Translations
        {
            get
            {
                return _translations;
            }
            set
            {
                _translations = value;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets the attribute translation.
        /// </summary>
        /// <param name="languageID">The language identifier.</param>
        /// <returns>The attribute translation.</returns>
        public virtual TLookupAttributeEntity Translation(int languageID)
        {
            return _translations.SingleOrDefault(x => x.Language.EntityID == languageID);
        }

        #endregion

        #region Utilities

        protected override void Disclose(IDictionary<String, Object> obj,
            DisclosureLevel level,
            IDictionary<String, String> options)
        {
            base.Disclose(obj, level, options);
            if(level >= DisclosureLevel.Base)
            {
                obj["DisplayOrder"] = DisplayOrder;
                obj["Flags"] = Flags.ToString();
                obj["Translations"] = (options.ContainsKey("TranslatedTo")
                    ? Translations.Where(x => options["TranslatedTo"].Contains(x.Language.Code))
                    : Translations)
                    .Map(x => x.Disclose(level, options));
            }
        }

        #endregion
    }

    /// <summary>
    /// The entity NHibernate mapping.
    /// </summary>
    public class LookupAttributeMapping : EntityMapping<LookupAttributeEntity>
    {
        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public LookupAttributeMapping()
        {
            Table("fwk_LookupAttributes");
            Id(x => x.EntityID, m =>
            {
                m.Column("AttributeID");
                m.Generator(Generators.Identity);
            });
            Property(x => x.DisplayOrder, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.Flags, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.CreatedOn, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.UpdatedOn, m =>
            {
                m.NotNullable(true);
            });
            Bag(x => x.Translations, m =>
            {
                m.Table("fwk_TCatalogAttributes");
                m.Key(k =>
                {
                    k.Column("AttributeID");
                    k.ForeignKey("FK_fwk_TCatalogAttributes_fwk_CatalogAttributes");
                    k.NotNullable(true);
                });
                m.Inverse(true);
                m.BatchSize(10);
            },
            a =>
            {
                a.OneToMany();
            });
            Cache(m =>
            {
                m.Usage(CacheUsage.ReadWrite);
            });
        }
    }
}
